//
//  HDGCustomCircleView.m
//  DGCircleDemo
//
//  Created by HunDegong on 2024/12/5.
//

#import "HDGCustomCircleView.h"

@interface HDGCustomCircleView()

@end

@implementation HDGCustomCircleView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _largeCircleRadius = 100;
        _smallCircleRadius = 0;
        _middleCircleRadius = 100;
        _largeCircleColor = [UIColor colorWithRed:16.0/255.0 green:18.0/255.0 blue:50.0/255.0 alpha:1.0];
        _smallCircleColor = [UIColor colorWithRed:16.0/255.0 green:18.0/255.0 blue:50.0/255.0 alpha:1.0];
        _middleCircleColor = [UIColor colorWithRed:37/255.0 green:71/255.0 blue:70/255.0 alpha:1];
        _lineColor = UIColor.whiteColor;
        _borderColor = [UIColor colorWithRed:97/255.0 green:101/255.0 blue:110/255.0 alpha:1];
        _lineWidth = 1.0;
        _minValue = 0;
        _maxValue = 100;
        
        // 初始化十字线的属性
        _crossLineWidth = 1.0; // 默认线宽
        _crossLineLength = 40.0; // 默认线长（可以根据需要调整）
        _crossLineColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0]; // 默认黑色
        
        // 初始化第一个光标图片
        _cursorImage = [UIImage imageNamed:@"left_point"];
        // 初始化第二个光标的图片
        _secondCursorImage = [UIImage imageNamed:@"second_point"];
        
        // 初始位置
        self.center = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
        self.cursorCenter = [self calculateCursorPositionWithX:0 Y:0];
        self.secondCursorCenter = [self calculateSecondCursorPositionWithX:0 Y:0];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 计算小圆和中圆的半径
    CGFloat smallCircleRadius = self.largeCircleRadius * (self.minValue / 100);
    CGFloat middleCircleRadius = self.largeCircleRadius * (self.maxValue / 100.0);
    
    // 绘制大圆
    CGRect largeCircleRect = CGRectMake(self.center.x - self.largeCircleRadius,
                                        self.center.y - self.largeCircleRadius,
                                        2 * self.largeCircleRadius,
                                        2 * self.largeCircleRadius);
    CGContextSetFillColorWithColor(context, self.largeCircleColor.CGColor);
    CGContextFillEllipseInRect(context, largeCircleRect);
    
    // 绘制中圆
    CGRect middleCircleRect = CGRectMake(self.center.x - middleCircleRadius,
                                         self.center.y - middleCircleRadius,
                                         2 * middleCircleRadius,
                                         2 * middleCircleRadius);
    CGContextSetFillColorWithColor(context, self.middleCircleColor.CGColor);
    CGContextFillEllipseInRect(context, middleCircleRect);
    
    // 绘制小圆
    CGRect smallCircleRect = CGRectMake(self.center.x - smallCircleRadius,
                                        self.center.y - smallCircleRadius,
                                        2 * smallCircleRadius,
                                        2 * smallCircleRadius);
    CGContextSetFillColorWithColor(context, self.smallCircleColor.CGColor);
    CGContextFillEllipseInRect(context, smallCircleRect);
    
    // 绘制大圆的边框
    CGContextSetStrokeColorWithColor(context, self.borderColor.CGColor);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextAddEllipseInRect(context, largeCircleRect);
    CGContextStrokePath(context);
    
    // 设置十字线的颜色
    CGContextSetStrokeColorWithColor(context, self.crossLineColor.CGColor);
    
    // 设置十字线的宽度
    CGContextSetLineWidth(context, self.crossLineWidth);
    
    // 绘制横线（穿过圆心，长度为 crossLineLength）
    CGFloat crossLineHalfLength = self.crossLineLength / 2.0;
    CGContextMoveToPoint(context, self.center.x - crossLineHalfLength, self.center.y);
    CGContextAddLineToPoint(context, self.center.x + crossLineHalfLength, self.center.y);
    
    // 绘制竖线（穿过圆心，长度为 crossLineLength）
    CGContextMoveToPoint(context, self.center.x, self.center.y - crossLineHalfLength);
    CGContextAddLineToPoint(context, self.center.x, self.center.y + crossLineHalfLength);
    
    // 描边路径以绘制十字线
    CGContextStrokePath(context);
    
    // 绘制校准光标图片（有则绘制）
    if (self.secondCursorImage) {
        
        // 先绘制连接校准光标的线
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextMoveToPoint(context, self.center.x, self.center.y);
        CGContextAddLineToPoint(context, self.secondCursorCenter.x, self.secondCursorCenter.y);
        CGContextStrokePath(context);
        
        // 再绘制校准光标图片
        [self.secondCursorImage drawAtPoint:CGPointMake(self.secondCursorCenter.x - self.secondCursorImage.size.width / 2, self.secondCursorCenter.y - self.secondCursorImage.size.height / 2)];
    }
    
    // 绘制光标图片（有则绘制）
    if (self.cursorImage) {
        
        // 先绘制连接线
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextMoveToPoint(context, self.center.x, self.center.y);
        CGContextAddLineToPoint(context, self.cursorCenter.x, self.cursorCenter.y);
        CGContextStrokePath(context);
        
        // 再绘制光标图片
        [self.cursorImage drawAtPoint:CGPointMake(self.cursorCenter.x - self.cursorImage.size.width / 2,
                                                  self.cursorCenter.y - self.cursorImage.size.height / 2)];
    }
}

- (CGPoint)calculateCursorPositionWithX:(CGFloat)x Y:(CGFloat)y {
    CGFloat distance = hypot(x, y);
    if (distance > self.largeCircleRadius) {
        x = x * (self.largeCircleRadius / distance);
        y = y * (self.largeCircleRadius / distance);
    }
    return CGPointMake(self.center.x + x, self.center.y + y);
}

- (void)setCursorPositionWithX:(CGFloat)x Y:(CGFloat)y {
    self.cursorCenter = [self calculateCursorPositionWithX:x Y:y];
    [self setNeedsDisplay];
}

- (CGPoint)calculateSecondCursorPositionWithX:(CGFloat)x Y:(CGFloat)y {
    // 根据需要计算第二个光标的位置
    // 这里可以使用与 calculateCursorPositionWithX:Y: 类似的方法，或者完全不同的逻辑
    CGFloat distance = hypot(x, y);
    if (distance > self.largeCircleRadius) {
        x = x * (self.largeCircleRadius / distance);
        y = y * (self.largeCircleRadius / distance);
    }
    return CGPointMake(self.center.x + x, self.center.y + y);
}
 
- (void)setSecondCursorPositionWithX:(CGFloat)x Y:(CGFloat)y {
    self.secondCursorCenter = [self calculateSecondCursorPositionWithX:x Y:y];
    [self setNeedsDisplay];
}

- (void)setMinValue:(CGFloat)minValue {
    _minValue = minValue;
    [self setNeedsDisplay];
}

- (void)setMaxValue:(CGFloat)maxValue {
    _maxValue = maxValue;
    [self setNeedsDisplay];
}

@end
